package com.heima.wemedia.Aliyunutil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest;
import com.aliyun.imageaudit20191230.models.ScanImageResponse;
import com.aliyun.tea.TeaModel;
import com.aliyun.teautil.Common;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class textimage {
    public static com.aliyun.imageaudit20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
        return new com.aliyun.imageaudit20191230.Client(config);
    }

    public static String  testimage(String pathUrl) throws Exception {
        String accessKeyId = "LTAI5tDJDzuzL3hCW8H6mQWd";
        String accessKeySecret = "UQ9PfoW2nLVBESJu3RH01Fwb4EcGvV";
        com.aliyun.imageaudit20191230.Client client = textimage.createClient(accessKeyId, accessKeySecret);
        // 场景二，使用任意可访问的url
        URL url = new URL(pathUrl);
        InputStream inputStream1 = url.openConnection().getInputStream();
        ScanImageAdvanceRequest.ScanImageAdvanceRequestTask task0 = new ScanImageAdvanceRequest.ScanImageAdvanceRequestTask();
//        task0.setDataId("uuid-xxxx-xxxx-123456");
//        task0.setImageTimeMillisecond(1L);
//        task0.setInterval(1);
//        task0.setMaxFrames(1);
        task0.setImageURLObject(inputStream1);
        List<ScanImageAdvanceRequest.ScanImageAdvanceRequestTask> taskList = new ArrayList<>();
        taskList.add(task0);
        List<String> sceneList = new ArrayList<>();
        sceneList.add("porn");
        com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest scanImageAdvanceRequest = new com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest()
                .setTask(taskList)
                .setScene(sceneList);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            ScanImageResponse scanImageResponse = client.scanImageAdvance(scanImageAdvanceRequest, runtime);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(scanImageResponse)));
            // 获取单个字段
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(scanImageResponse.body)));
            String jsonStr = Common.toJSONString(TeaModel.buildMap(scanImageResponse.body));
            // 解析为JSONObject
            JSONObject jsonObject = JSON.parseObject(jsonStr);

            // 逐层获取嵌套的JSON结构
            JSONObject data = jsonObject.getJSONObject("Data");
            JSONArray results = data.getJSONArray("Results");
            JSONObject firstResult = results.getJSONObject(0);
            JSONArray subResults = firstResult.getJSONArray("SubResults");
            JSONObject firstSubResult = subResults.getJSONObject(0);

            // 提取suggestion的值
            String suggestion = firstSubResult.getString("Suggestion");

            return suggestion;
        } catch (com.aliyun.tea.TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
            return "";
        }
    }
}
